package com.xuerhua.yxt.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.model.SchoolbusLine;
import com.xuerhua.yxt.model.SchoolbusStudentBoard;
import com.xuerhua.yxt.reqVo.SchoolbusQueryVO;
import com.xuerhua.yxt.reqVo.SchoolbusSignReqVO;
import com.xuerhua.yxt.service.ISchoolbusLineService;
import com.xuerhua.yxt.utils.StringUtil;

@Controller
public class SchoolbusController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SchoolbusController.class);
	
	@Autowired
	private ISchoolbusLineService schoolbusLineService;
	
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS)
	public String schoolbus(Model model) {
		AppUser appUser = getAppUser();
		List<SchoolbusLine> list = schoolbusLineService.selectLineByTeacherId(
				appUser.getTeacher().getId());
		if(list.size() == 1) {
			model.addAttribute("isFollowTeacher", true);
			return linePage(model, list.get(0).getId());
		}
		return listPage(model);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/list")
	public String listPage(Model model) {
		List<SchoolbusLine> list = schoolbusLineService.selectList();
		model.addAttribute("list", list);
		return "schoolbus/list";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/line/{lineId}")
	public String linePage(Model model, @PathVariable Integer lineId) {
		AppUser appUser = getAppUser();
		model.addAttribute("isSchoolbusManager", 
				StringUtil.isExist(appUser.getTeacher().getPosition(), "6006"));
		model.addAttribute("lineId", lineId);
		return "schoolbus/lineDetail";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/myClass")
	public String linePage(Model model) {
		return "schoolbus/myClass";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/lineDetail")
	@ResponseBody Object lineDetail(Integer lineId) {
		return schoolbusLineService.selectViewDetailByLineId(lineId);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/studentList")
	@ResponseBody Object studentList(SchoolbusQueryVO vo) {
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		return schoolbusLineService.selectSchoolbusListView(vo);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/studentBoard")
	@ResponseBody Object studentBoard(SchoolbusStudentBoard board) {
		try {
			if(schoolbusLineService.isStartCar(board.getLineId())){
				return new RspResult(Status.START_CAR_HAD);
			}
			AppUser appUser = getAppUser();
			board.setSchoolId(appUser.getTeacher().getSchoolId());
			board.setSchoolName(appUser.getTeacher().getSchoolName());
			board.setBoardTime(new Date());
			schoolbusLineService.addAndUpdateStudentBoard(board);
			return new RspResult(Status.SUCCESS, board);
		} catch (Exception e) {
			logger.error("----studentBoard error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/boardRemind")
	@ResponseBody Object boardRemind(Integer lineId) {
		try {
			if(schoolbusLineService.isStartCar(lineId)){
				return new RspResult(Status.START_CAR_HAD);
			}
			AppUser appUser = getAppUser();
			SchoolbusQueryVO vo = new SchoolbusQueryVO();
			vo.setLineId(lineId);
			vo.setTeacherName(appUser.getTeacher().getName());
			int count = schoolbusLineService.isTeacherLine(vo);
			if(count == 0) return new RspResult(Status.SCHOOLBUS_OVER);
			schoolbusLineService.addSchoolbusBoardNoticeParent(lineId);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----boardRemind error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.SCHOOLBUS + "/teacherPhone")
	@ResponseBody Object teacherPhone(Integer grade, Integer clazz) {
		return schoolbusLineService.selectTeacherPhoneForSchoolbus(grade, clazz);
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.SCHOOLBUS)
	public String studentSchoolBus(Model model) {
		AppUser appUser = getAppUser();
		List<SchoolbusLine> myList = schoolbusLineService.selectLineForParent(appUser.getOpenid());
		List<SchoolbusLine> linelist = schoolbusLineService.selectList();
		model.addAttribute("myList", myList);
		model.addAttribute("lineList", linelist);
		return "schoolbus/myList";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.SCHOOLBUS + "/siteList")
	@ResponseBody Object studentLineDetail(Integer lineId) {
		return schoolbusLineService.selectSiteListByLineId(lineId);
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.SCHOOLBUS + "/studentBoard")
	@ResponseBody Object studentListForParent(Integer lineId) {
		AppUser appUser = getAppUser();
		SchoolbusQueryVO vo = new SchoolbusQueryVO();
		vo.setLineId(lineId);
		vo.setOpenid(appUser.getOpenid());
		return schoolbusLineService.selectStudentListByParam(vo);
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.SCHOOLBUS + "/studentSign")
	@ResponseBody Object studentSign(SchoolbusSignReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setOpenid(appUser.getOpenid());
			return schoolbusLineService.addSchoolbusSign(reqVO);
		} catch (Exception e) {
			logger.error("--studentSign error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
